package com.yanlin.servicedriveruser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yanlin.internalcommon.dto.DriverCarBindingRelationship;
import com.yanlin.internalcommon.dto.ResponseResult;
import com.yanlin.servicedriveruser.mapper.DriverCarBindingRelationshipMapper;
import com.yanlin.servicedriveruser.service.IDriverCarBindingRelationshipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.yanlin.internalcommon.constant.CommonStatusEnum.*;
import static com.yanlin.internalcommon.constant.DriverCarConstant.DRIVER_CAR_BIND;
import static com.yanlin.internalcommon.constant.DriverCarConstant.DRIVER_CAR_UNBIND;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yanlin
 * @since 2025-06-14
 */
@Service
public class DriverCarBindingRelationshipServiceImpl
        extends ServiceImpl<DriverCarBindingRelationshipMapper, DriverCarBindingRelationship>
        implements IDriverCarBindingRelationshipService {

    @Autowired
    private DriverCarBindingRelationshipMapper driverCarBindingRelationshipMapper;

    @Override
    public ResponseResult bind(DriverCarBindingRelationship driverCarBindingRelationship) {
        // 检查是否已绑定
        QueryWrapper<DriverCarBindingRelationship> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bind_state", DRIVER_CAR_BIND);
        queryWrapper.eq("car_id", driverCarBindingRelationship.getCarId());
        queryWrapper.eq("driver_id", driverCarBindingRelationship.getDriverId());
        Integer count = driverCarBindingRelationshipMapper.selectCount(queryWrapper);
        if (count > 0) {
            return ResponseResult.fail(DRIVER_CAR_BIND_EXISTS.getCode(),
                    DRIVER_CAR_BIND_EXISTS.getValue());
        }
        // 司机已绑定
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bind_state", DRIVER_CAR_BIND);
        queryWrapper.eq("driver_id", driverCarBindingRelationship.getDriverId());
        count = driverCarBindingRelationshipMapper.selectCount(queryWrapper);
        if (count > 0) {
            return ResponseResult.fail(DRIVER_BIND_EXISTS.getCode(),
                    DRIVER_BIND_EXISTS.getValue());
        }

        // 车辆已绑定
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bind_state", DRIVER_CAR_BIND);
        queryWrapper.eq("car_id", driverCarBindingRelationship.getCarId());
        count = driverCarBindingRelationshipMapper.selectCount(queryWrapper);
        if (count > 0) {
            return ResponseResult.fail(CAR_BIND_EXISTS.getCode(),
                    CAR_BIND_EXISTS.getValue());
        }

        driverCarBindingRelationship.setBindState(DRIVER_CAR_BIND);
        driverCarBindingRelationship.setBindingTime(LocalDateTime.now());
        driverCarBindingRelationshipMapper.insert(driverCarBindingRelationship);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult unbind(DriverCarBindingRelationship driverCarBindingRelationship) {
        // 检查是否绑定
        Map<String, Object> map = new HashMap<>();
        map.put("bind_state", DRIVER_CAR_BIND);
        map.put("car_id", driverCarBindingRelationship.getCarId());
        map.put("driver_id", driverCarBindingRelationship.getDriverId());
        List<DriverCarBindingRelationship> list = driverCarBindingRelationshipMapper.selectByMap(map);
        if (list.isEmpty()) {
            return ResponseResult.fail(DRIVER_CAR_BIND_NOT_EXISTS.getCode(),
                    DRIVER_CAR_BIND_NOT_EXISTS.getValue());
        }
        // 绑定
        DriverCarBindingRelationship binding = list.get(0);
        binding.setBindState(DRIVER_CAR_UNBIND);
        binding.setUnBindingTime(LocalDateTime.now());
        driverCarBindingRelationshipMapper.updateById(binding);
        return ResponseResult.success();
    }
}
